package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.WorkerFileTypeEnum;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.supervision.convert.GzryFileConvert;
import com.engine4cloud.tdf.supervision.dto.GzryFileDTO;
import com.engine4cloud.tdf.supervision.dto.query.GzryFileQueryDTO;
import com.engine4cloud.tdf.supervision.entity.GzryFileEntity;
import com.engine4cloud.tdf.supervision.mapper.GzryFileMapper;
import com.engine4cloud.tdf.supervision.service.GzryFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工作人员（社工或志愿者）-文件表
 *
 * @author wenjie
 * @date 2024-04-15 11:38:33
 */
@Slf4j
@Service
public class GzryFileServiceImpl extends ServiceImpl<GzryFileMapper, GzryFileEntity> implements GzryFileService {
    @Override
    public List<GzryFileDTO> findList(GzryFileQueryDTO dto) {
        return list(wrapperList(dto)).stream().map(GzryFileConvert.INSTANCE::convert).toList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveFile(GzryFileQueryDTO dto, List<GzryFileDTO> photoList, TdfUser user) {
        this.saveBatch(GzryFileConvert.INSTANCE.createList(dto, photoList, user));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateFile(GzryFileQueryDTO dto, List<GzryFileDTO> photoList, TdfUser user) {
        List<GzryFileDTO> existedList = this.findList(dto);

        List<GzryFileEntity> preSaveFileList = this.preSaveFileList(dto, photoList, dto.getFileType(), user);
        if (CollectionUtils.isNotEmpty(preSaveFileList)) {
            this.saveBatch(preSaveFileList);
        }

        List<Long> preDeleteFileList = this.preDeleteFileList(existedList, photoList);
        if (CollectionUtils.isNotEmpty(preDeleteFileList)) {
            this.removeBatchByIds(preDeleteFileList);
        }
    }

    private List<Long> preDeleteFileList(List<GzryFileDTO> existedList, List<GzryFileDTO> modelFileList) {
        if (CollectionUtils.isEmpty(existedList)) {
            return Collections.emptyList();
        }

        Map<Long, GzryFileDTO> modelFileMap = modelFileList.stream().filter(model -> Objects.nonNull(model.getId())).collect(Collectors.toMap(GzryFileDTO::getId, Function.identity(), (v1, v2) -> v1));
        return existedList.stream().map(GzryFileDTO::getId).filter(id -> !modelFileMap.containsKey(id)).toList();
    }


    private List<GzryFileEntity> preSaveFileList(GzryFileQueryDTO dto, List<GzryFileDTO> photoList, WorkerFileTypeEnum fileType, TdfUser user) {
        if (CollectionUtils.isEmpty(photoList)) {
            return Collections.emptyList();
        }

        return photoList.stream().filter(file -> Objects.isNull(file.getId())).map(file -> GzryFileConvert.INSTANCE.create(dto.getRybm(), file, fileType, user)).toList();
    }

    private LambdaQueryWrapper<GzryFileEntity> wrapperList(GzryFileQueryDTO query) {
        LambdaQueryWrapper<GzryFileEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(GzryFileEntity::getId, v));
        Queries.accept(query.getRybm(), v -> wrapper.eq(GzryFileEntity::getRybm, v));
        Queries.accept(query.getFileType(), v -> wrapper.eq(GzryFileEntity::getFileType, v));
        return wrapper;
    }

}